package com.yangxk.kadmin.web.module.gen.util;

import com.yangxk.kadmin.common.util.ValidUtil;
import com.yangxk.kadmin.generator.field.Field;
import com.yangxk.kadmin.generator.rules.FormElement;
import com.yangxk.kadmin.generator.rules.Match;
import com.yangxk.kadmin.web.module.gen.entity.TableColumn;
import com.yangxk.kadmin.web.module.gen.types.JavaTypeResolver;

import java.util.ArrayList;
import java.util.List;

public class GenUtil {
    private GenUtil(){}

    public static List<Field> transformColumn2Show(List<TableColumn> tcs, String primaryKey) {
        if (ValidUtil.isEmpty(tcs))
            return null;
        List<Field> result = new ArrayList<>(tcs.size());
        Field field = null;

        JavaTypeResolver javaTypeResolver = new JavaTypeResolver();
        for (TableColumn tc : tcs) {
            field = new Field();
            field.setColumnName(tc.getColumnName());
            field.setJdbcType(javaTypeResolver.getJdbcTypeName(tc.getDataType()));
//            field.setColumnSize(tc.getColumnSize());
            field.setComment(tc.getRemarks());
            field.setPropertyType(javaTypeResolver.getJavaTypeName(tc.getDataType()));
            field.setPropertyShortType(javaTypeResolver.getJavaTypeShortName(tc.getDataType()));
            field.setPropertyName(getCamelCaseString(tc.getColumnName()));

            // 主键
            if (tc.getColumnName().equalsIgnoreCase(primaryKey))
                field.setPrimaryKey(true);

            // 添加/更新字段(表单)
            if (tc.getColumnName().equalsIgnoreCase("create_by")
                    || tc.getColumnName().equalsIgnoreCase("create_time")
                    || tc.getColumnName().equalsIgnoreCase("update_by")
                    || tc.getColumnName().equalsIgnoreCase("update_time")
                    || tc.getColumnName().equalsIgnoreCase("del_flag")) {
                field.setEdit(false);
            }

            // 列表字段
            if (tc.getColumnName().equalsIgnoreCase("id")
                    || tc.getColumnName().equalsIgnoreCase("create_by")
                    || tc.getColumnName().equalsIgnoreCase("create_time")
                    || tc.getColumnName().equalsIgnoreCase("update_by")
                    || tc.getColumnName().equalsIgnoreCase("update_time")
                    || tc.getColumnName().equalsIgnoreCase("del_flag")
                    || tc.getColumnName().equalsIgnoreCase("remark")) {
                field.setListShow(false);
            }

            // 查询字段
            if (tc.getColumnName().contains("name")
                    || tc.getColumnName().equalsIgnoreCase("title")) {
                field.setQuery(true);
                field.setMatch(Match.like);
            }

            // 验证类型

            // 字典类型

            // 控件类型
            if (field.getPropertyShortType().equalsIgnoreCase("Date"))
                field.setFormElement(FormElement.DATE);
            if (field.getPropertyName().equalsIgnoreCase("remark"))
                field.setFormElement(FormElement.TEXTAREA);

            result.add(field);
        }
        return result;
    }

    /**
     * 将数据库列名称转为驼峰命名
     * create by yangxk 2018/9/28 15:30
     */
    public static String getCamelCaseString(String inputString) {
        StringBuilder sb = new StringBuilder();
        boolean nextUpperCase = false;
        for (int i = 0; i < inputString.length(); i++) {
            char c = inputString.charAt(i);

            switch (c) {
                case '_':
                case '-':
                case '@':
                case '$':
                case '#':
                case ' ':
                case '/':
                case '&':
                    if (sb.length() > 0) {
                        nextUpperCase = true;
                    }
                    break;

                default:
                    if (nextUpperCase) {
                        sb.append(Character.toUpperCase(c));
                        nextUpperCase = false;
                    } else {
                        sb.append(Character.toLowerCase(c));
                    }
                    break;
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String str = "user_name";
        System.out.println(getCamelCaseString(str));
    }
}
